<?php

class SearchController extends ControllerBase
{

    public function indexAction()
    {
    	$this->view->setMainView('index');
		$this->view->setVar('page_name', 'Search');
		$this->view->setVar('content', 'search/index');
		$this->view->setVar('title', 'Search');

        $searchString = $_GET['searchString'];
        $searchBy = $_GET['searchBy'];
        

        switch($searchBy)
        {
            case "People":
                 $id = UserAccountTb::findFirst(array(
                    "conditions"=>"CONCAT(fname,' ',lname) = ?0",
                    'bind'=>array(0=>$searchString)
                ));
                if($id){
                    header("Location: ".BASE_URI.'profile?bhd='.$id->id);
                }     
            break;

            case "Company":
                $id = ClientInfo::findFirst(array(
                    "conditions"=>"name = ?0",
                    'bind'=>array(0=>$searchString)
                ));
                if($id){
                    header("Location: ".BASE_URI.'reviews?cname='.$searchString);
                }
            break;
            case "Group":
                $id = Groups::findFirst(array(
                    "conditions"=>"name = ?0",
                    'bind'=>array(0=>$searchString)
                ));
                if($id){
                    header("Location: ".BASE_URI.'group?ghd='.$id->id);
                }
            break;
        }
                   
        $data = UserAccountTb::search($searchBy,$searchString);

        $points = array();
        foreach ($data as $value) {
            $x = array(
                    $value->fname.' '.$value->lname,
                    $value->lat,
                    $value->long
                );
            $points[] = implode(",", $x);
        }
        
        $this->view->setVar("points",implode("&", $points));  
        $this->view->setVar("data",$data);
    }

    public function listCitiesAction()
    {

        $result = array();
        $list = array();
    	$cities = UserAddressTb::listCities();


    	foreach ($cities as $value) {
    		$list[] = $value->city;
    	}
    	
    	$result['data'] = $list;
    	echo json_encode($result);
    	$this->view->disable(); 	
    }
    public function listPeopleAction()
    {

        $names = UserAccountTb::listNames();
        $result = array();

        $result2 = array();

        foreach ($names as $value) {
            $list = array();
            $list['value'] = $value->fname.' '.$value->lname;
            $list['label'] = $value->fname.' '.$value->lname;
            $list['desc'] = (empty($value->city)) ? "" : $value->city.', '.'Philippines';
            $list['icon'] = IMAGE_URL_SMALL.$value->profile_pic;
            $list['href'] = BASE_URI.'profile?bhd='.$value->id;
            $result2[] = $list;
        }
      
        $names = ClientInfo::listNames();
        $result = array();
        foreach ($names as $value) {
            $list = array();
            $list['value'] = $value->name;
            $list['label'] = $value->name;
            $list['desc'] = "Company";
            $list['icon'] = IMAGE_URL_SMALL.$value->profile_pic;
            $list['href'] = BASE_URI.'reviews?cname='.$value->name;
            $result2[] = $list;
        }
       
        $names = Groups::listNames();
        

        foreach ($names as $value) {
            $list = array();
            $list['value'] = $value->name;
            $list['label'] = $value->name;
            $list['desc'] = ($value->privacy == 1) ? "Private Group" : "Public Group";
            $list['icon'] = IMAGE_URL_SMALL.$value->icon;
            $list['href'] = BASE_URI.'group?ghd='.$value->id;   
            $result2[] = $list;
        }
        
        $cities = UserAddressTb::listCities();

        foreach ($cities as $value) {
            $list = array();
            $list['value'] = $value->city;
            $list['label'] = $value->city;
            $list['desc'] = "City";
            $list['href'] = BASE_URI.'search?searchBy=Map&searchString='.$value->city;   
            $list['icon'] = 'http://res.cloudinary.com/behomebased-ph/image/upload/c_scale,h_23,w_15/v1425566228/Marker_ugxcke.png';
            $result2[] = $list;   
            
        }   
        $result['data'] = $result2;
        echo json_encode($result);
        $this->view->disable();     
    }

    public function listCompanyAction(){
        $names = ClientInfo::listNames();
        $result = array();

        $result2 = array();

        foreach ($names as $value) {
            $list = array();
            $list['value'] = $value->name;
            $list['label'] = $value->name;
            $list['desc'] = $value->name;
            $list['icon'] = IMAGE_URL_SMALL.$value->profile_pic;
            $result2[] = $list;
        }
        
        $result['data'] = $result2;
        echo json_encode($result);
        $this->view->disable(); 
    }

    public function listGroupsAction(){
        $result = array();

        $result2 = array();

        $names = Groups::listNames();
        
        foreach ($names as $value) {
            $list = array();
            $list['value'] = $value->name;
            $list['label'] = $value->name;
            $list['desc'] = $value->name;
            $list['icon'] = IMAGE_URL_SMALL.$value->icon;
            $result2[] = $list;
        }
        
        $result['data'] = $result2;
        echo json_encode($result);
        $this->view->disable();    
    }
}

